﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class HospitalPSL_Total : BasePage
    {
        HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
        string source = "";
        public HospitalPSL_Total()
        {
            this.ModuleNo = Module.医院统计分析.ToString();
            this.PageOperateType = OperateType.医院汇总统计.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindArea();
                this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                DataBind();
                //GetStatistics();
            }
        }

        /// <summary>
        /// 金额统计
        /// by：张雄 
        /// </summary>
        //void GetStatistics()
        //{
        //    double purchaseAmountTotal = 0;//采购总金额
        //    double wareHouseAmountTotal = 0;//入库总金额
        //    double distributeAmountTotal = 0;//配送总金额

        //    for (int i = 0; i < GridView1.Rows.Count; i++)     //遍历表的行数
        //    {
        //        purchaseAmountTotal += double.Parse(GridView1.Rows[i].Cells[2].Text);
        //        wareHouseAmountTotal += double.Parse(GridView1.Rows[i].Cells[5].Text);
        //        distributeAmountTotal += double.Parse(GridView1.Rows[i].Cells[3].Text);
        //    }

        //    this.lblCount.Text = "采购总金额为：" + purchaseAmountTotal.ToString() + "；配送总金额为：" + distributeAmountTotal.ToString() + "；入库总金额为：" + wareHouseAmountTotal.ToString() + "；";
        //}

        /// <summary>
        /// 绑定数据方法
        /// YD
        /// 2011-3-30 19:18
        /// </summary>
        private void DataBind()
        {
            double purchaseAmountTotal = 0;//采购总金额
            int t = 0;
            //查询条件    by张雄  2011.5.4
            string orderby = this.OrderFileds.SelectedItem.Value + "  " + this.OrderType.SelectedItem.Value;
            string areaid = AreaControl1.SelectedAreaID;
            string hosptialname = HZ.Supervise.DAL.DAL_Helper.Replace(txtHospitalName.Text.ToString());
            if (ddlSortSource.SelectedValue != "全部")     //判断是否等于‘全部’  ‘是’不加条件‘否’加入采购类型的查询条件
            {
                source = this.ddlSortSource.SelectedValue;
            }

            System.Data.DataTable dt = iEC.ExecutePagePSL_HOS(areaid, hosptialname, source, orderby, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);

            System.Data.DataTable dt1 = iEC.ExecutePagePSL_HOS(areaid, hosptialname, source, orderby, 1, 0, out t);

            AspNetPager1.RecordCount = t;

            gvwPSLInfo.PageSize = AspNetPager1.PageSize;

            this.gvwPSLInfo.DataSource = dt;
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");

            this.gvwPSLInfo.DataBind();

            for (int i = 0; i <  dt1.Rows.Count ; i++)     //遍历表的行数
            {
                purchaseAmountTotal += Convert.ToDouble(dt1.Rows[i][2]);
            }
            this.lblCount.Text = "采购总金额为：" + purchaseAmountTotal.ToString() + "；";
        }

        /// <summary>
        /// 查询数据事件
        /// 董辉
        /// 2011-3-30 19:17
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btn_chaxun_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;

         }

        /// <summary>
        /// 分页事件
        /// 董辉
        /// 2011-3-30 19:19
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataBind();
            //GetStatistics();
        }
        /// <summary>
        /// 导出数据事件
        /// 董辉
        /// 2011-3-30 19:19
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            //modify    by  张雄    
            int t = 0;

            string orderby = this.OrderFileds.SelectedItem.Value + "  " + this.OrderType.SelectedItem.Value;
             string areaid = AreaControl1.SelectedAreaID;
            string hosptialname = HZ.Supervise.DAL.DAL_Helper.Replace(txtHospitalName.Text.ToString());
            if (ddlSortSource.SelectedValue != "全部")     //判断是否等于‘全部’  ‘是’不加条件‘否’加入采购类型的查询条件
            {
                source = this.ddlSortSource.SelectedValue;
            }


            System.Data.DataTable dt = iEC.ExecutePagePSL_HOS(areaid, hosptialname, source, orderby, 0, 0, out t);

            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");


            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }
    }
}
